import functools
from typing import Tuple

from mg_app_framework import get_handler, TaskKey, get_organization
from motor.core import AgnosticCollection

collection_func_list = []


def mongo_decorator(func):
    """创建索引可能需要一些额外参数: https://www.mongodb.com/docs/v4.0/reference/method/db.collection.createIndex/
    常见的索引参数
    background	在后台创建索引，不阻塞其他数据库操作
    unique	创建唯一索引，确保字段的值唯一
    sparse	创建稀疏索引，只为存在索引字段的文档创建索引，减少索引大小
    expireAfterSeconds	设置索引的过期时间，用于自动删除过期的文档
    用法: 索引参数形式:
        单字段索引: (索引字段, 顺序, (可选option, 额外参数))
        联合索引: [(索引字段1, 顺序), (索引字段2, 顺序)), (可选option, 额外参数)]
    示例:
    @mongo_decorator
    async def get_db_scan_history() -> AgnosticCollection:
         database_name = f'{get_organization()}_aps'
         collection_name = 'scan_history'
         index_list = [("mark", 1),
                      [('workorder_code', 1), ('device_code', 1), ('_id', -1), {'background': True}],
         ]
         return database_name, collection_name, index_list
    """
    collection_func_list.append(func)

    @functools.wraps(func)
    def wrap_func() -> AgnosticCollection:
        handler = get_handler(TaskKey.idi_server)
        database_name, collection_name, _ = func()
        return handler[database_name][collection_name]

    return wrap_func


# 不需要创建索引的直接像下面这样返回 物联网数据表
def get_idi_data() -> AgnosticCollection:
    table_name = 'datas'
    db = f'idi_{get_organization()}'
    return get_handler(TaskKey.idi_server)[db].get_collection(table_name)


@mongo_decorator
def get_container_status() -> AgnosticCollection:
    # 可重复使用的容器
    db = f'idi_{get_organization()}_upper_pc'
    table_name = 'container_status'
    index_list = [
        ("product_list.mark", 1),
        ("container_mark", 1),
    ]
    return db, table_name, index_list


@mongo_decorator
def get_container_mark_config() -> AgnosticCollection:
    # 容器码规则定义
    db = f'idi_{get_organization()}_upper_pc'
    table_name = 'container_mark_config'
    index_list = [

    ]
    return db, table_name, index_list


@mongo_decorator
def get_test():
    db = f'{get_organization()}_aps'
    table_name = 'test'
    index_list = []
    return db, table_name, index_list


@mongo_decorator
def get_code_generation_record() -> Tuple[str, str, list]:
    # 存放分切极卷码生成条码和传入的所有原料条码的绑定关系
    db = f"idi_{get_organization()}_upper_pc"
    table_name = 'code_generation_record'
    index_list = [
        ("parent_code", 1),
        ("child_codes", 1),
    ]
    return db, table_name, index_list


@mongo_decorator
def get_rule_code_record() -> Tuple[str, str, list]:
    # 存放蓝胶码,电芯码的绑定关系
    db = f"idi_{get_organization()}_upper_pc"
    table_name = 'rule_code_record'
    index_list = [
        ("code", 1),
        ("serial_no", 1),
        ("create_time", -1),
    ]
    return db, table_name, index_list


def get_workorder_record() -> AgnosticCollection:
    db = f'{get_organization()}_workorder'
    table_name = 'workorder_record'
    return get_handler(TaskKey.idi_server)[db].get_collection(table_name)
